package com.blb.controller;

import com.blb.dto.*;
import com.blb.service.ZTypeService;
import com.blb.service.ZUserService;
import com.blb.service.ZZhuboService;
import com.blb.utils.LayUIDataTable;
import com.blb.utils.Status;
import com.blb.vo.Zhuboinfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

@Controller
@RequestMapping("/zhubo")
@CrossOrigin
public class ZhuboController {

    @Autowired
    private ZZhuboService zZhuboService;

    @Autowired
    private ZUserService zUserService;

    @Autowired
    private ZTypeService zTypeService;


    /**
     *
     * @param model
     * @param zZhubo
     * @return
     */
    @PostMapping("/zhuboinfo")
    @ResponseBody
    public LayUIDataTable tobezhubo(Model model,@RequestBody ZZhubo zZhubo){
        ZUser zUser = zUserService.selectByPrimaryKey(zZhubo.getUid());
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");  //uuid作为主播秘钥
        zZhubo.setNichen(zUser.getNichen());
        zZhubo.setMiyao(uuid);
        zZhubo.setGuanzhunum(0);
        Random random = new Random();
        int i = random.nextInt(50000);
        zZhubo.setZrenqi(i);
        zZhubo.setRoomid((4340+zZhubo.getUid())+"");
        zZhuboService.insertSelective(zZhubo);  //新增主播

        ZZhuboExample zZhuboExample = new ZZhuboExample();
        ZZhuboExample.Criteria criteria = zZhuboExample.createCriteria();
        criteria.andUidEqualTo(zZhubo.getUid());
        List<ZZhubo> zZhubos = zZhuboService.selectByExample(zZhuboExample);
        Integer zid = zZhubos.get(0).getZid();
        Zhuboinfo zhuboinfo = new Zhuboinfo(uuid,(4340+zZhubo.getUid())+"",zid);
        zUser.setIszhubo(0+"");
        zUserService.updateByPrimaryKey(zUser);  //更新用户的主播标识
        LayUIDataTable dt = new LayUIDataTable();
        dt.setData(zhuboinfo);
        dt.setCode(Status.SUCCESS);
        dt.setCount(0);
        dt.setMsg("认证主播成功!");
        return dt;
    }

    @GetMapping("/yue/{uid}")
    @ResponseBody
    public LayUIDataTable yue(Model model,@PathVariable("uid") String uid){
        LayUIDataTable dt = new LayUIDataTable();
        dt.setData(0);
        if(uid!=null&&!uid.equals("null"))
        {
        ZUser zUser = zUserService.selectByPrimaryKey(Integer.parseInt(uid));
        BigDecimal moneybag = zUser.getMoneybag();
        dt.setData(moneybag);
        }
        dt.setCode(Status.SUCCESS);
        dt.setCount(0);
        dt.setMsg("查询成功!");
        return dt;
    }

    @RequestMapping("/queryzhubotid/{tid}/{page}/{pageSize}")
    @ResponseBody
    public LayUIDataTable queryzhubotid(Model model, @PathVariable("tid") int tid,@PathVariable("page") int page,@PathVariable("pageSize") int pageSize)
    {
        LayUIDataTable dt = new LayUIDataTable();
        ZZhuboExample zZhuboExample = new ZZhuboExample();
        ZZhuboExample.Criteria criteria = zZhuboExample.createCriteria();
        criteria.andTidEqualTo(tid);
        PageHelper.startPage(page,pageSize);                //分页
        List<ZZhubo> zZhubos = zZhuboService.selectByExample(zZhuboExample);
        PageInfo pageInfo = new PageInfo(zZhubos);
        ArrayList<Object> objects = new ArrayList<>();
        for (ZZhubo zhubo:zZhubos){
            ZUser zUser = zUserService.selectByPrimaryKey(zhubo.getUid());
            objects.add(zUser);
        }
        String tname = zTypeService.selectByPrimaryKey(tid).getTname();
        dt.setCode(Status.SUCCESS);
        dt.setCount(1);
        dt.setMsg(tname);
        dt.setData(zZhubos);
        dt.setData2(objects);
        dt.setData3(pageInfo.getPrePage());
        dt.setData4(pageInfo.getNextPage());
        dt.setData5(pageInfo.getPageNum());
        return dt;
    }


    @RequestMapping("/querybyzid/{zid}")
    @ResponseBody
    public LayUIDataTable querybuzid(Model model, @PathVariable("zid") int zid)
    {
        //pic/user/zanwu.png
        ZUser zUser1 = new ZUser();
        zUser1.setUpicture("pic/user/zanwu.png");
        zUser1.setNichen("暂无上榜");
        LayUIDataTable dt = new LayUIDataTable();
        ZZhubo zhubo = zZhuboService.selectByPrimaryKey(zid);
        String weekstart = zhubo.getWeekstart();
        List<ZUser> objects = new ArrayList();
        if (weekstart!=null&&weekstart!=""){
            String replace = weekstart.replace("[", "");
            String replace2 = replace.replace("]", "");
            String[] split = replace2.split(",");
            for (int i=0;i<split.length;i++) {
                ZUser zUser2 = zUserService.selectByPrimaryKey(Integer.parseInt(split[i].trim()));
                objects.add(zUser2);
            }
            for (int j=0;j<3-split.length;j++)
            {
                objects.add(zUser1);
            }
        }
        else {
            objects.add(zUser1);
            objects.add(zUser1);
            objects.add(zUser1);
        }
        ZUser zUser = zUserService.selectByPrimaryKey(zhubo.getUid());
        String tname = zTypeService.selectByPrimaryKey(zhubo.getTid()).getTname();
        dt.setCode(Status.SUCCESS);
        dt.setCount(1);
        dt.setMsg(tname);
        dt.setData(zhubo);
        dt.setData2(zUser.getNichen());
        dt.setData3(objects);
        return dt;
    }

    @RequestMapping("/indextype")
    @ResponseBody
    public LayUIDataTable queryindextype(Model model)  //查询人气前三的非顶级分类下的4名主播展示在前台
    {
        LayUIDataTable dt = new LayUIDataTable();
        ZTypeExample zTypeExample = new ZTypeExample();
        zTypeExample.setOrderByClause("trenqi DESC limit 0,3");
        ZTypeExample.Criteria criteria = zTypeExample.createCriteria();
        ZTypeExample.Criteria criteria1 = criteria.andPidNotEqualTo(0);
        ZZhuboExample zZhuboExample1 = new ZZhuboExample();
        ZZhuboExample.Criteria criteria3 = zZhuboExample1.createCriteria(); //查询首页右侧5个直播间的信息
        List<ZType> zTypes = zTypeService.selectByExample(zTypeExample);
        ArrayList<Object> objects = new ArrayList<>();
        ArrayList<Object> tnames = new ArrayList<>();
        ArrayList<Object> tidalls = new ArrayList<>();
        for(ZType zType:zTypes) {
            int tid = zType.getTid();
            tnames.add(zType.getTname());
            tidalls.add(zType.getTid());
            criteria3.andTidNotEqualTo(tid);   //右侧直播间不包括下方12个直播间
            ZZhuboExample zZhuboExample = new ZZhuboExample();
            zZhuboExample.setOrderByClause("zrenqi DESC limit 0,4");
            ZZhuboExample.Criteria criteria2 = zZhuboExample.createCriteria();
            criteria2.andTidEqualTo(tid);
            List<ZZhubo> zZhubos = zZhuboService.selectByExample(zZhuboExample);
            objects.add(zZhubos);
        }
        List<ZZhubo> zZhubos = zZhuboService.selectByExample(zZhuboExample1);
        int size = zZhubos.size();
        int small = 0;
        int big = size-1;
        int length = big-small+1;
        int n = 5;
        List<ZZhubo> zZhubos5 = new ArrayList<>();
        int[] seed = new int[length];
        for (int i = 0; i < length; i++) {
            seed[i] = small + i;
        }
        int[] ranArr = new int[n];
        Random ran = new Random();
        for (int i = 0; i < n; i++) {
            int j = ran.nextInt(length - i);
            ranArr[i] = seed[j];
            seed[j] = seed[length - 1 - i];
        }
        for (int k=0;k<ranArr.length;k++)
        {
            ZZhubo zhubo = zZhubos.get(ranArr[k]);
            zZhubos5.add(zhubo);
        }
        dt.setCode(Status.SUCCESS);
        dt.setCount(objects.size());
        dt.setMsg("查询成功!");
        dt.setData(objects);
        dt.setData2(tnames);
        dt.setData3(zZhubos5);
        dt.setData4(tidalls);
        return dt;
    }



}
